package com.wise.service.cms.content.uploadresource;

import com.wise.entities.content.Resource;
import com.wise.framework.dao.SqlSelect;
import com.wise.framework.daomodel.DaoPageList;
import com.wise.framework.restful.PageResultList;
import com.wise.summer.v1.cms.content.uploadresource.GetUploadResource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.summerframework.model.SummerService;
import org.summerframework.model.SummerServiceBean;

/**
 * @author Pitaya
 * @date 2019-11-15
 */

@Service
@SummerService
public class GetUploadResourceService implements SummerServiceBean<GetUploadResource> {
    @Value("${azure.file.url}")
    private String url;

    @Override
    public void sum(GetUploadResource summer) {
        SqlSelect selectResource = SqlSelect.selec("*")
                .fromClassName(Resource.class)
                .eq(Resource._groupId, summer.getGroupId())
                .eqLang(Resource._lang)
                .like(!StringUtils.isEmpty(summer.getFileName()), Resource._originResourceName,
                        summer.getFileName() + "%")
                .order_by(Resource.UPLOAD_TIME + " desc");
        PageResultList<Resource> resultList = DaoPageList.s(Resource.class, selectResource,
                summer.getStart(), summer.getLimit());
        if (StringUtils.isNotEmpty(url)) {
            for (Resource resource : resultList.getList()) {
                int index = resource.getPath().lastIndexOf("/");
                String fileName = resource.getPath().substring(index + 1);
                resource.setPath(url + fileName);
                resource.setResourcePath(url + fileName);
            }
        }
        summer.setSummerResult(resultList);
    }
}
